package com.cskaoyan.typehandler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 数据库varchar转Java String[]
 * 2022/9/9 21:22 Austin Hsu
 */


@MappedTypes({String[].class})
@MappedJdbcTypes(JdbcType.VARCHAR)
public class StringArrayTypeHandler implements TypeHandler<String[]> {
//    ObjectMapper objectMapper = new ObjectMapper();
//    @SneakyThrows
//    @Override
//    public void setParameter(PreparedStatement preparedStatement, int i, String[] strings, JdbcType jdbcType) throws SQLException {
//        String value = objectMapper.writeValueAsString(strings);//也可以用Gson
//        preparedStatement.setString(i,value);
//    }
//
//    @Override
//    public String[] getResult(ResultSet resultSet, String columnName) throws SQLException {
//        String result = resultSet.getString(columnName);
//        return transfer(result);
//    }
//
//    @Override
//    public String[] getResult(ResultSet resultSet, int columnIndex) throws SQLException {
//        String result = resultSet.getString(columnIndex);
//        return transfer(result);
//    }
//
//    @Override
//    public String[] getResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
//        String result = callableStatement.getString(columnIndex);
//        return transfer(result);
//    }
//
//    @SneakyThrows
//    private String[] transfer(String result) {
//        String[] array = new String[0];
//        if (result == null || "".equals(result)) {
//            return array;
//        }
//        array = objectMapper.readValue(result, String[].class);
//        return array;
//    }

    ObjectMapper objectMapper = new ObjectMapper();

    // 输入映射过程
    // insert into market_user (id,username,password,role_ids) values (?,?,?,?)
    @Override
    public void setParameter(PreparedStatement preparedStatement, int index, String[] strings, JdbcType jdbcType) throws SQLException {
        // StringBuffer resultBuffer = new StringBuffer();
        // resultBuffer.append("[");
        // for (String integer : strings) {
        //     resultBuffer.append(integer).append(",");
        //        // }
        String value = null;
        try {
            value = objectMapper.writeValueAsString(strings);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        // 为第几个占位符提供的值是什么
        preparedStatement.setString(index,value);
    }

    // 输出映射过程
    @Override
    public String[] getResult(ResultSet resultSet, String columName) throws SQLException {
        // 获得结果
        String result = resultSet.getString(columName);
        return transfer(result);
    }

    @Override
    public String[] getResult(ResultSet resultSet, int index) throws SQLException {
        // 获得结果
        String result = resultSet.getString(index);
        return transfer(result);
    }

    @Override
    public String[] getResult(CallableStatement callableStatement, int i) throws SQLException {
        // 获得结果
        String result = callableStatement.getString(i);
        return transfer(result);
    }

    private String[] transfer(String result) {
        String[] strings = new String[0];
        // 使用jackson将字符串转换String[]
        try {
            strings = objectMapper.readValue(result, String[].class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return strings;
    }
}
